package com.ruoyi.service.service.impl;

import java.util.List;

import com.ruoyi.archive.domain.ArchiveCar;
import com.ruoyi.archive.mapper.ArchiveCarMapper;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.mapper.ServiceCarMoveMapper;
import com.ruoyi.service.domain.ServiceCarMove;
import com.ruoyi.service.service.IServiceCarMoveService;

/**
 * 小区挪车Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-21
 */
@Service
public class ServiceCarMoveServiceImpl implements IServiceCarMoveService 
{
    private static final Logger log = LoggerFactory.getLogger(ServiceCarMoveServiceImpl.class);

    @Autowired
    private ServiceCarMoveMapper serviceCarMoveMapper;
    @Autowired
    private ArchiveCarMapper archiveCarMapper;
    /**
     * 查询小区挪车
     * 
     * @param id 小区挪车主键
     * @return 小区挪车
     */
    @Override
    public ServiceCarMove selectServiceCarMoveById(Long id)
    {
        return serviceCarMoveMapper.selectServiceCarMoveById(id);
    }

    /**
     * 查询小区挪车列表
     * 
     * @param serviceCarMove 小区挪车
     * @return 小区挪车
     */
    @Override
    public List<ServiceCarMove> selectServiceCarMoveList(ServiceCarMove serviceCarMove)
    {
        return serviceCarMoveMapper.selectServiceCarMoveList(serviceCarMove);
    }

    /**
     * 新增小区挪车
     * 
     * @param serviceCarMove 小区挪车
     * @return 结果
     */
    @Override
    public int insertServiceCarMove(ServiceCarMove serviceCarMove)
    {
        ArchiveCar archiveCar;
        if(serviceCarMove.getCarId() != null) {
            archiveCar = archiveCarMapper.selectArchiveCarById(serviceCarMove.getCarId());
            serviceCarMove.setCarPlate(archiveCar.getCarPlate());
            serviceCarMove.setPersonId(archiveCar.getPersonId());

            if(serviceCarMove.getDeptId() == null){
                serviceCarMove.setDeptId(archiveCar.getDeptId());
            }
        }else{
            archiveCar = archiveCarMapper.selectArchiveCarByPlate(serviceCarMove.getCarPlate());
            if(archiveCar != null) {
                serviceCarMove.setCarId(archiveCar.getId());

                if(serviceCarMove.getDeptId() == null){
                    serviceCarMove.setDeptId(archiveCar.getDeptId());
                }
                if(serviceCarMove.getPersonId() == null){
                    archiveCar.setPersonId(archiveCar.getPersonId());
                }
            }
        }
        serviceCarMove.setCreateTime(DateUtils.getNowDate());
        return serviceCarMoveMapper.insertServiceCarMove(serviceCarMove);
    }

    /**
     * 修改小区挪车
     * 
     * @param serviceCarMove 小区挪车
     * @return 结果
     */
    @Override
    public int updateServiceCarMove(ServiceCarMove serviceCarMove)
    {
        serviceCarMove.setUpdateTime(DateUtils.getNowDate());
        return serviceCarMoveMapper.updateServiceCarMove(serviceCarMove);
    }

    /**
     * 批量删除小区挪车
     * 
     * @param ids 需要删除的小区挪车主键
     * @return 结果
     */
    @Override
    public int deleteServiceCarMoveByIds(Long[] ids)
    {
        return serviceCarMoveMapper.deleteServiceCarMoveByIds(ids);
    }

    /**
     * 删除小区挪车信息
     * 
     * @param id 小区挪车主键
     * @return 结果
     */
    @Override
    public int deleteServiceCarMoveById(Long id)
    {
        return serviceCarMoveMapper.deleteServiceCarMoveById(id);
    }
}
